package cc.cix.hq.org.controller;

import cc.cix.hq.common.core.entity.Page;
import cc.cix.hq.common.core.entity.QueryRequest;
import cc.cix.hq.common.core.entity.constant.StringConstant;
import cc.cix.hq.common.core.entity.org.User;
import cc.cix.hq.common.core.utils.HqUtil;
import cc.cix.hq.org.dto.query.QueryUserReq;
import cc.cix.hq.org.dto.request.RegisterReq;
import cc.cix.hq.org.dto.request.UserReq;
import cc.cix.hq.org.service.IUserService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author cix
 */
@Api(tags = "用户管理")
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/user")
public class UserController {

    private final IUserService userService;
    private final PasswordEncoder passwordEncoder;

    @ApiOperation("用户列表")
    @PreAuthorize("hasAuthority('user:list')")
    @GetMapping
    public Page<User> listUser(QueryRequest queryRequest, QueryUserReq dto) {
        User user = BeanUtil.toBean(dto, User.class);
        return userService.listUser(user, queryRequest);
    }

    @ApiOperation("添加用户")
    @PreAuthorize("hasAuthority('user:add')")
    @PostMapping
    public void addUser(@Valid @RequestBody UserReq dto) {
        User user = BeanUtil.toBean(dto, User.class);
        userService.createUser(user);
    }

    @ApiOperation("注册用户")
    @PostMapping("/register")
    public void registerUser(@Valid @RequestBody RegisterReq dto) {
        User user = BeanUtil.toBean(dto, User.class);
        userService.registerUser(user);
    }

    @ApiOperation("更新用户")
    @PreAuthorize("hasAuthority('user:update')")
    @PutMapping
    public void updateUser(@Valid @RequestBody UserReq dto) {
        User user = BeanUtil.toBean(dto, User.class);
        userService.updateUser(user);
    }

    @ApiOperation("删除用户")
    @PreAuthorize("hasAuthority('user:delete')")
    @DeleteMapping("/{userIds}")
    public void deleteUsers(@PathVariable String userIds) {
        List<String> ids = StrUtil.splitTrim(userIds, StringConstant.COMMA);
        userService.deleteUserByIds(ids);
    }

    @ApiOperation("更新用户个人信息")
    @PutMapping("/profile")
    public void updateProfile(@Valid @RequestBody UserReq dto) {
        User user = BeanUtil.toBean(dto, User.class);
        userService.updateProfile(user);
    }

    @ApiOperation("获取当前用户最新信息")
    @GetMapping("/my")
    public User getCurrentUser() {
        String mobile = HqUtil.getCurrentUser().getMobile();
        return userService.getUserByMobile(mobile);
    }

    @ApiOperation("更新密码")
    @PutMapping("/password")
    public void updatePassword(String password) {
        userService.lambdaUpdate()
                .eq(User::getUsername, HqUtil.getCurrentUser().getUsername())
                .set(User::getPassword, passwordEncoder.encode(password))
                .update();
    }

    @ApiOperation("重置密码")
    @PreAuthorize("hasAuthority('user:reset')")
    @PutMapping("/password/reset")
    public void resetPassword(String usernames) {
        userService.lambdaUpdate()
                .in(User::getUsername, StrUtil.splitTrim(usernames, StringConstant.COMMA))
                .set(User::getPassword, passwordEncoder.encode(User.DEFAULT_PASSWORD))
                .update();
    }

}
